import java.io.File;
import java.io.IOException;

import static com.zl.core.base.utilTime.getLast12Months;

public class FileIf {



        public static void main(String[] args) {

/*
            File file = new File("d:\\test_file.txt");
            judeFileExists(file);
*/
            String[] last12Months = getLast12Months("2015-12");

            for(int i=0;i<12;i++)
            {
                File dir = new File("d:/test_dir/2015/"+last12Months[i]);
                judeDirExists(dir);
                System.out.println("last12Months[i] = " + last12Months[i]);
            }


            last12Months = getLast12Months("2016-12");

            for(int i=0;i<12;i++)
            {
                File dir = new File("d:/test_dir/2016/"+last12Months[i]);
                judeDirExists(dir);
                System.out.println("last12Months[i] = " + last12Months[i]);
            }


            last12Months = getLast12Months("2017-12");

            for(int i=0;i<12;i++)
            {
                File dir = new File("d:/test_dir/2017/"+last12Months[i]);
                judeDirExists(dir);
                System.out.println("last12Months[i] = " + last12Months[i]);
            }


            last12Months = getLast12Months("2018-12");

            for(int i=0;i<12;i++)
            {
                File dir = new File("d:/test_dir/2018/"+last12Months[i]);
                judeDirExists(dir);
                System.out.println("last12Months[i] = " + last12Months[i]);
            }

        }

        // 判断文件是否存在
        public static void judeFileExists(File file) {

            if (file.exists()) {
                System.out.println("file exists");
            } else {
                System.out.println("file not exists, create it ...");
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }

        // 判断文件夹是否存在
        public static void judeDirExists(File file) {

            if (file.exists()) {
                if (file.isDirectory()) {
                    System.out.println("dir exists");
                } else {
                    System.out.println("the same name file exists, can not create dir");
                }
            } else {
                System.out.println("dir not exists, create it ...");
                file.mkdir();
            }

        }

    }

